const print = (title = '默认标题', headData = [], mainData = []) => {
  var printWindow = window.open('', '_blank')
  printWindow.document.open()
  printWindow.document.write('<html><head><title>Print</title></head><body>')
  printWindow.document.write(`<h1 style="text-align:center;">${title}</h1>`)
  let headItem = ''
  let mainItem = ''
  headData.forEach((item) => {
    headItem += `<th style="border: 1px dotted black;">${item}</th>`
  })
  mainData.forEach((item) => {
    let td = ''
    for (let key in item) {
      td += `<td style="border: 1px dotted black;">${item[key]}</td>`
    }
    mainItem += `<tr>
                    ${td}
                  </tr>`
  })

  let body = `<table style="width: 100%; border: 1px solid #000; border-collapse: collapse;">
                <thead >
                  <tr style="border: 1px dotted black;">
                    ${headItem}
                  </tr>
                </thead>
                <tbody style="text-align:center;">
                  ${mainItem}
                </tbody>
              </table>`
  printWindow.document.write(body)
  printWindow.document.write('</body></html>')
  printWindow.document.close()
  printWindow.print()
}

export default print
